package com.googlecode.jtoy.patterns.observer;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

/**
 * @author shangxj
 * @date 6:43:57 PM, Jul 16, 2011
 */
public class ObserverTest {

	private MockTimeSource source;
	private MockTimeSink sink;

	@Before
	public void before() {
		source = new MockTimeSource();
		sink = new MockTimeSink(source);

		source.registerObserver(sink);
	}

	@Test
	public void testTimeChange() {
		source.setTime(3, 4, 5);
		assertSinkEquals(sink, 3, 4, 5);

		source.setTime(7, 8, 9);
		assertSinkEquals(sink, 7, 8, 9);
	}

	@Test
	public void testMultiSink() {
		MockTimeSink sink2 = new MockTimeSink(source);
		source.registerObserver(sink2);

		source.setTime(11, 12, 13);
		assertSinkEquals(sink, 11, 12, 13);
		assertSinkEquals(sink2, 11, 12, 13);
	}

	private void assertSinkEquals(MockTimeSink sink, int hours, int minutes, int seconds) {
		assertEquals(hours, sink.getHours());
		assertEquals(minutes, sink.getMinutes());
		assertEquals(seconds, sink.getSeconds());
	}
}
